package yja.gzw.app.adapter

import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.request.RequestOptions
import com.tencent.imsdk.v2.V2TIMConversation
import com.tencent.qcloud.tuicore.TUIConstants
import com.tencent.qcloud.tuikit.tuichat.classicui.page.TUIC2CChatActivity
import yja.gzw.app.R
import yja.gzw.app.activity.BianjizhuanquActivity
import yja.gzw.app.activity.DindanchangajiexiangqingActivity
import yja.gzw.app.activity.FabidingdanActivity
import yja.gzw.app.chengjiafang.ChengjiadaiquerengActivity
import yja.gzw.app.chengjiafang.Chengjiechengxiao
import yja.gzw.app.chengjiafang.ChengjiedaifahuoActivity
import yja.gzw.app.chengjiafang.ChengjiedaipingjiaActivity
import yja.gzw.app.chengjiafang.ChengjiedaishouhuoActivity
import yja.gzw.app.model.UserData
import yja.gzw.app.model.bean.OrderDatachengjie
import yja.gzw.app.viewmodel.Mainviewmodel

class ChengjieAdapter  (private val content: Context, private val list: MutableList<OrderDatachengjie>): RecyclerView.Adapter<RecyclerView.ViewHolder>(){
    private var view: View?=null
    val viewmodel= Mainviewmodel()
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        view= LayoutInflater.from(parent.context).inflate(R.layout.item_view_dindanshow,parent,false)
        return ViewHodeView(view!!)
    }

    override fun getItemCount(): Int {
       return list.size
    }


    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {

        if(holder is ViewHodeView){
            holder.itemView.setOnClickListener {
                //todo 跳到不同的页面

                if(list[position].status==2){

                    val inten=Intent(content, ChengjiadaiquerengActivity::class.java)
                    inten.putExtra("ids",list[position].id.toString())
                    content.startActivity(inten)

                }else if(list[position].status==3){
                    val inten=Intent(content, ChengjiedaifahuoActivity::class.java)
                    inten.putExtra("ids",list[position].id.toString())
                    content.startActivity(inten)

                }else if(list[position].status==4){
                    val inten=Intent(content, ChengjiedaishouhuoActivity::class.java)
                    inten.putExtra("ids",list[position].id.toString())
                    content.startActivity(inten)

                } else if(list[position].status==5){
                    val inten=Intent(content, ChengjiedaipingjiaActivity::class.java)
                    inten.putExtra("ids",list[position].id.toString())
                    content.startActivity(inten)

                } else if(list[position].status==-1||list[position].status==-2||list[position].status==-3){
                    val inten=Intent(content, Chengjiechengxiao::class.java)
                    inten.putExtra("ids",list[position].id.toString())
                    content.startActivity(inten)
                }



            }





        when(list[position].status){
            0->{
                holder.zhuangtai.text="未支付"
                holder.zhuangtai.setTextColor(content.resources.getColor(R.color.hong))
            }
            1->{
                holder.zhuangtai.text="发布中"
                holder.zhuangtai.setTextColor(content.resources.getColor(R.color.huangh))
            }
            2->{
                holder.zhuangtai.text="待确认"
                holder.zhuangtai.setTextColor(content.resources.getColor(R.color.huangh))

            }
            3->{
                holder.zhuangtai.text="待发货"
                holder.zhuangtai.setTextColor(content.resources.getColor(R.color.hei))
                holder.qufabu.visibility=View.VISIBLE
            }
            4->{
                holder.zhuangtai.text="待收货"
                holder.zhuangtai.setTextColor(content.resources.getColor(R.color.hong))
            }
            5->{
                holder.zhuangtai.text="待评价"
                holder.zhuangtai.setTextColor(content.resources.getColor(R.color.hei))
                holder.pingjia.visibility=View.GONE

            }
            -1->{
                holder.zhuangtai.setTextColor(content.resources.getColor(R.color.hong))
                holder.zhuangtai.text="\n撤销订单待第二方确认"
            }
            -2->{

                holder.zhuangtai.setTextColor(content.resources.getColor(R.color.hong))
                holder.zhuangtai.text="\n撤销订单待后台确认"
            }
            -3->{

                holder.zhuangtai.setTextColor(content.resources.getColor(R.color.hong))
                holder.zhuangtai.text="\n撤销订单已完成"
            }




        }





            holder.dindanhao.text=list[position].order_no
            if(list[position].type=="1"){

                holder. zhuanuqleixing.text="三元专区"

            }else if(list[position].type=="2"){

                holder. zhuanuqleixing.text="四元专区"
            }else{
                holder. zhuanuqleixing.text="精品视频"

            }
            holder. zhuanquname.text=list[position].title
            if(list[position].release_type==1){
                holder.fabumoshi.text="自己使用ai生成视频"

            }else if(list[position].release_type==2){
                holder.fabumoshi.text="第二方使用ai生成视频"
            }else{
                holder.fabumoshi.text="不使用ai生成视频"
            }
//
            val requestOptions = RequestOptions.bitmapTransform(CircleCrop())
            holder.name.text=list[position].userinfo.username
            Glide.with(content).load("http://117ai.32.hzgqapp.com/"+list[position].userinfo.avatar)
                .apply(requestOptions)
                .into(holder.touxiang)

            holder.sixing.setOnClickListener {
                val intent = Intent(content, TUIC2CChatActivity::class.java)
                intent.putExtra(TUIConstants.TUIChat.CHAT_ID,list[position].userinfo.id.toString());
                intent.putExtra(TUIConstants.TUIChat.CHAT_TYPE, V2TIMConversation.V2TIM_C2C)
                content.startActivity(intent)


            }

        }

    }
    inner class ViewHodeView(itemView: View):RecyclerView.ViewHolder(itemView){

        val dindanhao: TextView =itemView.findViewById(R.id.dindanbiaohao)
        val zhuanuqleixing: TextView =itemView.findViewById(R.id.cishu)
        val zhuanquname: TextView =itemView.findViewById(R.id.zhuanquname)
        val fabumoshi: TextView =itemView.findViewById(R.id.fabupingtai)
        val touxiang: ImageView =itemView.findViewById(R.id.usertouxiang)
        val name: TextView =itemView.findViewById(R.id.name)
        val sixing: TextView =itemView.findViewById(R.id.sixing)

        val zhuangtai: TextView =itemView.findViewById(R.id.zhuangtai)
        val pingjia: TextView =itemView.findViewById(R.id.pingjia)

        val qufabu: TextView =itemView.findViewById(R.id.qufabu)

    }

}